﻿/***
Welcome to Blackjack!
### Dealer's Hand
? of ?
Ten of Clubs

### Your Hand (13)
Seven of Spades
Six of Diamonds

(H)it or (S)tand? h
### Dealer's Hand
? of ?
Ten of Clubs

### Your Hand (20)
Seven of Spades
Six of Diamonds
Seven of Clubs

(H)it or (S)tand? s
### Dealer's Hand(20)
Ten of Diamonds
Ten of Clubs

### Your Hand (20)
Seven of Spades
Six of Diamonds
Seven of Clubs

PUSH - YOU LOSE! (Welcome to Vegas, baby!) >:-]
***/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CeeSharpBlackJack
{
    class BlackJack
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Blackjack!");

            Deck deck = new StandardDeck();
            Hand playerHand = new StandardHand();
            Hand dealerHand = new StandardHand();

            // initialize the deck with all 52 cards
            foreach(CeeSharpBlackJack.PlayingCard.Rank rank in Enum.GetValues(typeof(CeeSharpBlackJack.PlayingCard.Rank))) {
                foreach(CeeSharpBlackJack.PlayingCard.Suit suit in Enum.GetValues(typeof(CeeSharpBlackJack.PlayingCard.Suit))) {
                    deck.addCard(new PlayingCard(rank, suit));
                }
            }

            // ensure that the deck is sufficiently randomized
            deck.shuffle();

            // deal two cards to each hand
            playerHand.addCard(deck.deal());
            dealerHand.addCard(deck.deal());
            playerHand.addCard(deck.deal());
            dealerHand.addCard(deck.deal());

            // show the current game state to the player
            Console.WriteLine("### Dealer's Hand");
            Console.WriteLine(dealerHand.asObscuredString());
            Console.WriteLine("### Your Hand ("+playerHand.getValue()+")");
            Console.WriteLine(playerHand.asString());

            // allow the user to stand or hit until bust or 21
            while(playerHand.getValue() < 21) {
                String response = queryHitOrStand();
                if(response.Equals("h")) {
                    playerHand.addCard(deck.deal());
                    Console.WriteLine("### Dealer's Hand");
                    Console.WriteLine(dealerHand.asObscuredString());
                    Console.WriteLine("### Your Hand ("+playerHand.getValue()+")");
                    Console.WriteLine(playerHand.asString());
                } else {
                    break;
                }
            }

            // if the player has exceeded 21, taunt them and end
            if(playerHand.getValue() > 21) {
                Console.WriteLine("YOU BUSTED!! :-(");
                Console.ReadLine();
                return;
            }

            // allow the dealer to hit or stand until bust or 21
            while(dealerHand.getValue() < 16) {
                dealerHand.addCard(deck.deal());
            }
        
            Console.WriteLine("### Dealer's Hand("+dealerHand.getValue()+")");
            Console.WriteLine(dealerHand.asString());
            Console.WriteLine("### Your Hand ("+playerHand.getValue()+")");
            Console.WriteLine(playerHand.asString());

            // if the dealer has exceeded 21, congratulate the player
            if(dealerHand.getValue() > 21) {
                Console.WriteLine("DEALER BUSTED!! :-)");
                Console.ReadLine();
                return;
            }

            // if the dealer's hand is more valuable than the player's hand, taunt the player
            if(playerHand.getValue() < dealerHand.getValue()) {
                Console.WriteLine("DEALER WINS! :-P");
            } else if (playerHand.getValue() > dealerHand.getValue()) {
                Console.WriteLine("YOU WIN! :-O");
            } else {
                Console.WriteLine("PUSH - YOU LOSE! (Welcome to Vegas, baby!) >:-]");
            }
            Console.ReadLine();

        }

        private static String queryHitOrStand() {
            String response = "";
            while(!response.ToLower().Equals("h") && !response.ToLower().Equals("s")) {
                Console.Write("(H)it or (S)tand? ");
                response = Console.ReadLine();
            }
            return response;
        }

    }
}
